package cc.shimeng.fucloud.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.ai.chat.client.advisor.api.AdvisedRequest;
import org.springframework.ai.chat.client.advisor.api.AdvisedResponse;
import org.springframework.ai.chat.client.advisor.api.StreamAroundAdvisor;
import org.springframework.ai.chat.client.advisor.api.StreamAroundAdvisorChain;
import reactor.core.publisher.Flux;

public class LoggingAdvisor implements StreamAroundAdvisor {

    @Override
    public Flux<AdvisedResponse> aroundStream(AdvisedRequest advisedRequest, StreamAroundAdvisorChain chain) {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.enable(SerializationFeature.INDENT_OUTPUT); // 格式化输出

        try {
            String requestJson = objectMapper.writeValueAsString(advisedRequest);
            System.out.println("Request: " + requestJson);
        } catch (Exception e) {
            System.err.println("Failed to serialize advisedRequest to JSON: " + e.getMessage());
        }

        return chain.nextAroundStream(advisedRequest);
    }

    @Override
    public int getOrder() {
        return 0;
    }

    @Override
    public String getName() {
        return this.getClass().getName();
    }
}